﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using HYBY.Task.Service;
using HYBY.WebFrameWork;
using Autofac;
using Autofac.Integration.Mvc;
using FluentValidation.Attributes;
using FluentValidation.Mvc;

/*命名空间位置说明：
 * 系统命名空间放第一位；
 * 解决方案内部引用放第二位；
 * 解决方案外部，系统内部引用放在第三位；
 * 第三方开源组件放第四位；
 * 第三方商业组件放第五位
 */

namespace Web
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            Initializer();
        }

        void Initializer()
        {
            //Redis Init
            //RedisManager.Init();
            //Dapper Init
            ServiceManager.Initialize();

            //Ioc
            var builder = new ContainerBuilder();

            ServiceManager.RegisterService(builder, true);
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
            ValueProviderFactories.Factories.Add(new JsonArrayValueProviderFactory());

            // FluentValidation设置
            DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
            ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
        }
    }
}
